<?php

namespace App\Api\Fibrpro;
use App\Domain\User\User as UserDomain;
use PhalApi\Api;


/**
 * 通用API接口
 *
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class Index extends Api
{

    public function getRules()
    {
        return array(
            'select'       => array(
                'page'  => array('name' => 'page', 'require' => true, 'desc' => '当前页'),
                'size'  => array('name' => 'size', 'require' => true, 'desc' => '每页显示多少'),
                'table' => array('name' => 'table', 'require' => false, 'desc' => '表名'),
            ),
            "update"       => array(
                'id'    => array('name' => 'id', 'require' => false, 'desc' => 'id'),
                'arr'   => array('name' => 'arr', 'require' => true, 'desc' => 'JSON数据'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
            ),
            "update_where" => array(
                'id'    => array('name' => 'id', 'require' => false, 'desc' => 'id'),
                'arr'   => array('name' => 'arr', 'require' => true, 'desc' => 'JSON数据'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
                'where' => array('name' => 'where', 'require' => false, 'desc' => '更新条件'),
            ),
            "delete"       => array(
                'id'    => array('name' => 'id', 'require' => false, 'desc' => 'id'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
            ),
            "deleteSoft"       => array(
                'id'    => array('name' => 'id', 'require' => false, 'desc' => 'id'),
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
            ),
            "select_where" => array(
                'table' => array('name' => 'table', 'require' => true, 'desc' => '表名'),
                'page'  => array('name' => 'page', 'require' => true, 'desc' => '当前页'),
                'size'  => array('name' => 'size', 'require' => true, 'desc' => '每页显示多少'),
                'order' => array('name' => 'order', 'require' => false, 'desc' => '排序'),
                'where' => array('name' => 'where', 'require' => true, 'desc' => '查询条件'),
            )
        );
    }

 

    /**
     * 按条件更新
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function update_where()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        //接收以json传入数据
        //$rs = file_get_contents('php://input');
        $data = json_decode($this->arr, true);

        $id = $this->id;
        $table = $this->table;
        $data['update_time'] = date("Y-m-d H:i:s",time());

        //如果有条件就对条件进行更新
        $where = $this->where;
        if ($where){
            $where = json_decode($this->where, true);
            $One = \PhalApi\DI()->notorm->$table->where($where)->fetchOne();
            if ( $One ){
                return \PhalApi\DI()->notorm->$table->where($where)->update($data);
            }
        }

        //如果传了id 就对id就行更新
        if ( $id ){
            // 对该id进行更新
            return \PhalApi\DI()->notorm->$table
                ->where("id", $id)
                ->update($data);
        }

        // 创建一个创建时间
        $data['create_time'] = date("Y-m-d H:i:s",time());
        // 因为没有id所以新进一个数据
        return \PhalApi\DI()->notorm->$table->insert($data);
    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function select_where()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers:*');

        return \App\getInfoByPageByWhere($this);
    }

    /**
     * 删除接口
     * @desc 更新id删除接口
     * @return array data 删除状态
     * @exception 400 非法请求，参数传递错误
     */
    public function delete()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\deleteById($this);
    }

    /**
     * 软删除接口
     * @desc 更新id删除接口
     * @return array data 删除状态
     * @exception 400 非法请求，参数传递错误
     */
    public function deleteSoft()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\deleteByIdSoft($this);
    }

    /**
     * 更新及创建->接口
     * @desc 更新及新建接口服务
     * @return array data 数据内容
     * @return int count 当前条件下总数
     * @exception 400 非法请求，参数传递错误
     */
    public function update()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');
        return \App\updateByIdThis($this);
    }

    /**
     * 分页读取
     * @desc 按分页读取数据接口服务
     * @return array data 数据内容
     * @return int count 当前条件下总数
     * @exception 400 非法请求，参数传递错误
     */
    public function select()
    {
        // 最简单的处理方式
        header('Access-Control-Allow-Origin: *');
        header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
        header('Access-Control-Allow-Headers: *');

//        header('Access-Control-Allow-Headers:x-requested-with,content-type,requesttype');
        return \App\getInfoByPage($this);
    }
}
